package com.quanyue.haircut.shop.service.impl;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.haircut.domain.entity.Coupon;
import com.quanyue.haircut.shop.mapper.CouponMapper;
import com.quanyue.haircut.shop.service.CouponService;
import com.vhuomai.mybatis.mapper.page.Page;
import com.vhuomai.mybatis.mapper.page.PageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    private CouponMapper couponMapper;

    @Override
    public Coupon createCoupon(Coupon coupon) {
        coupon.setCreateTime(LocalDateTime.now());
        coupon.setDelFlag(false);
        couponMapper.insert(coupon);
        return coupon;
    }

    @Override
    public void deleteCoupon(Integer couponId) {
        Coupon check = couponMapper.findOne(couponId);
        AssertUtils.notNull(check, "优惠券信息校检出错");
        check.setDelFlag(true);
        couponMapper.update(check);
    }

    @Override
    public Page <Coupon> couponList(PageRequest request, Integer merchantNo) {
        request.setAsc(false);
        request.setOrderBy("create_time");
        return couponMapper.findByMerchantNoValid(request, merchantNo);
    }
}
